const BaseController = require("./base/base");
const Result = require("../utils/result");
const logsModel = require("../model/logs");
const { getTokenUser, isAdmin } = require("../middlewares/auth");
class LogsController extends BaseController {
  constructor() {
    super();
  }
  getAll(req, res, next) {
    setTimeout(async ()=>{
      try {
        let { pagesize = 10, page = 1, user_id } = req.query;
        let { user_id: userid, role: isadmin = "" } = getTokenUser(req);
        if (!isAdmin(isadmin)) user_id = userid;
        if (!pagesize) pagesize = 10;
        if (!page) page = 1;
        let count = await logsModel.getCount({ ...req.query, pagesize, page, user_id });
        let ret = await logsModel.getAll({ ...req.query, pagesize, page, user_id });
        res.json(Result.ok("success", { total: count[0].count, list: ret }));
      } catch (err) {
        res.json(Result.error(err));
      }
    })
  }
}
module.exports = new LogsController().resolve();